using UnityEngine;

public class RigidbodyRender
{
	public static HingeJoint2D hingle;

	public Rigidbody2D body;

	public Transform render;

	public SpriteRenderer spriteRender;

	public Transform zombie;

	public float floorPosition;

	public Vector3 slideRagdoll;

	public Collider2D[] collidersBody;

	public RigidbodyRender(Rigidbody2D body, SpriteRenderer spriteRender, Transform zombie, float floorPosition)
	{
		this.body = body;
		render = spriteRender.transform;
		this.spriteRender = spriteRender;
		Vector3 position = zombie.position;
		slideRagdoll = new Vector3(0f, position.y - floorPosition);
		this.zombie = zombie;
		this.floorPosition = floorPosition;
		collidersBody = body.gameObject.GetComponents<Collider2D>();
	}

	public void Enable(Transform renderParent, Transform bodyParent)
	{
		ref Vector3 reference = ref slideRagdoll;
		Vector3 position = zombie.position;
		reference.y = position.y - floorPosition;
		if (!body.simulated)
		{
			for (int i = 0; i < collidersBody.Length; i++)
			{
				collidersBody[i].enabled = true;
			}
			body.gameObject.name = zombie.name + ".body." + body.gameObject.name;
			render.gameObject.name += ".spr";
			body.transform.position = render.transform.position - slideRagdoll;
			body.transform.rotation = render.transform.rotation;
		}
		body.simulated = true;
		body.transform.SetParent(bodyParent);
		render.transform.SetParent(renderParent);
		hingle = body.gameObject.GetComponent<HingeJoint2D>();
		if (hingle == null)
		{
			body.angularVelocity = Mathf.Clamp(body.angularVelocity, -45f, 45f);
		}
		else
		{
			Vector3 eulerAngles = hingle.transform.rotation.eulerAngles;
			if (Mathf.Abs(Mathf.DeltaAngle(eulerAngles.z, 180f)) < 40f)
			{
				hingle.transform.rotation = hingle.connectedBody.transform.rotation;
				hingle.transform.Rotate(0f, 0f, 45f);
			}
		}
		hingle = null;
	}

	public void Disable()
	{
		body.simulated = false;
		for (int i = 0; i < collidersBody.Length; i++)
		{
			collidersBody[i].enabled = false;
		}
	}

	public void Update()
	{
		if (body.simulated)
		{
			render.position = body.transform.position + slideRagdoll;
			render.rotation = body.transform.rotation;
		}
	}
}
